package com.sinven.util;

import com.sinven.entity.BaseMessage;
import com.thoughtworks.xstream.XStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WechaUtil {

    public static Map<String,Object> XmlToMap (HttpServletRequest req) throws IOException, DocumentException {
        SAXReader reader=new SAXReader();
        Map<String,Object> map=new HashMap<String,Object>();
        //获取document对象
        Document doc=reader.read(req.getInputStream());
        Element root=doc.getRootElement();
        List<Element> list = root.elements();

        for (Element e :
                list) {
        map.put(e.getName(),e.getText());
            System.out.println(e.getName()+":"+e.getText());

        }


        return  map;





    }
    public static String outXml(BaseMessage baseMessage){
        XStream xstream = new XStream();
        xstream.alias("xml",BaseMessage.class);
        String str =xstream.toXML(baseMessage);

       return str ;
    }
}
